/*
 * UMLSynch RMI functionality test
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.rpc.test;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.googlecode.umlsynch.rpc.TClassDescriptor;
import com.googlecode.umlsynch.rpc.TEditorRpc;
import com.googlecode.umlsynch.rpc.TEditorRpcCaller;
import com.googlecode.umlsynch.rpc.TEditorRpcDispatcher;
import com.googlecode.umlsynch.rpc.TFinderRpc;
import com.googlecode.umlsynch.rpc.TFinderRpcCaller;
import com.googlecode.umlsynch.rpc.TFinderRpcDispatcher;
import com.googlecode.umlsynch.rpc.base.*;

public class TTestRpc {
	private TDispatcherManager dm;
	private TSystemDebug debug;
	private String filename = "d:/workspace/RPCGenerator/src/testrpc.xml" ;
	private Thread thread = null;
	private TConfig cfg;
	private boolean activatedFlag = false;

	public TTestRpc(TConfig eclipse, TConfig vpsde) {
		debug = new TSystemDebug();
		dm = new TDispatcherManager(debug);
		cfg = eclipse;
		TConfigReader cfgreader = new TConfigReader("C:/Temp/umlsynch.ini", debug);
		cfgreader.getConfig("EclipseIndexer");
		
	//	new ReflectionTestHer();
		
		if (thread == null) {
			thread = new Thread() {
				public void run() {
					
					TEditorRpc erpc = new TEditorRpc();
					TEditorRpcDispatcher erpcd = new TEditorRpcDispatcher((IDebug)debug, dm, erpc);
					TFinderRpc frpc = new TFinderRpc();
					TFinderRpcDispatcher frpcd = new TFinderRpcDispatcher(debug,dm, frpc);
					dm.register(erpcd);
					dm.register(frpcd);

					TIOHandler ioh = new TIOHandler(cfg, debug);
					if (!ioh.ServerConnect()) {
						debug.Error("Failed to open server socket");
						activatedFlag = true;
						return;
					}
					activatedFlag = true;
					while (ioh.accept()) {
						dm.process(ioh);	
					}

					debug.Error("Exit from dispatcher manager thread");
				}
			};
			thread.start();
		}
		
		while (!activatedFlag) {
			// do nothing
		}
		TIOHandler ioclient = new TIOHandler(cfg, debug);
	
		TEditorRpcCaller tec = new TEditorRpcCaller(ioclient, debug);
		TClassDescriptor cclass = new TClassDescriptor();
		cclass.name = "Class1";
		tec.OpenClass(cclass);
		
		TFinderRpcCaller tfc = new TFinderRpcCaller(ioclient, debug);
		TClassDescriptor[] res = tfc.FindClassByName(null, "class1");
		if (res != null) {
			debug.Note(res[0].name);
		}
		
/*
 * 		Some thoughts about testing RPC and improvments
 * 		There are a lot of RPC implementations like
 * 		RMI, CORBA, DCOM, SOAP etc ... 
 *  	but 
 *  	1) it is necessary to support one of them on user site
 *  	2) RMI, CORBA, DCOM suppose that you have some service for 
 *  	   components registration
 *  	3) it is wide specification for each product 
 *  	 
 *  	I think that it will be good to make a common solution 
 *  	which tries to use CORBA, but if it is unavailable 
 *  	it will try to start RMI, after that try to DCOM etc ... :(
 *  
 *   	
 * 		
		SAXParserFactory factory = SAXParserFactory.newInstance(); 
		factory.setValidating(false);
		
		XMLRpcParser  handler = new XMLRpcParser();
		
		try {
			factory.newSAXParser().parse(new File(filename), handler);
		} catch (SAXException e) {
				debug.Error("SAXException");
		} catch (IOException e) {
			debug.Error("IOException");
		} catch (ParserConfigurationException e) {
			debug.Error("ParserConfigurationException");
		} 
*/
	}
	protected void finalize() throws Throwable
	{
		if (thread != null) {
			thread.join();
		}
		super.finalize(); 
	}
}
